package com.jiayujie.httputils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipInputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.R.integer;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

public class ImageDownLoad {

	/**
	 * 图片下载工具类
	 * @param url  图片网络地址
	 * @param threadNum	线程数量	
	 * @param callback	回调方法
	 */
	public static void downLoadImage(final String url,final  int index,final CallBack callback){
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO 自动生成的方法存根
				if (url!=null&&!TextUtils.isEmpty(url)) {
					HttpClient client = new DefaultHttpClient();
					HttpParams  params = client.getParams();
					HttpConnectionParams.setConnectionTimeout(params, 30000);
					HttpConnectionParams.setSoTimeout(params, 30000);
					
					HttpGet httpGet = new HttpGet(url);
					HttpResponse response = null;
					InputStream stream = null;
					InputStream in = null;
					try {
						response = client.execute(httpGet);
						if (response.getStatusLine().getStatusCode()==HttpStatus.SC_OK) {
							HttpEntity entity= response.getEntity();
							String encoding = null;

							
							Header header = response.getFirstHeader("Content-Encoding");
							//获取内容的压缩格式
							if (header!= null) {
								encoding = header.getValue();
							}
							stream=entity.getContent();
							
							if (encoding!=null) {
								if (encoding.equals("gzip")) {
									in = new GZIPInputStream(stream);
								}else if (encoding.equals("zip")) {
									in = new ZipInputStream(stream);
								}else {
									throw new IllegalArgumentException("Unsupport   content-encoding:"+encoding);
								}
							}else{
								in = stream;
							}
							
							byte[] array = readStream(in);
							
//							byte[] array = EntityUtils.toByteArray(entity);
							 Bitmap bitmap = BitmapFactory.decodeByteArray(array, 0, array.length);
							 callback.returnResult(bitmap,index,url);
						}
					} catch (ClientProtocolException e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();
					} catch (IOException e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();
					}finally{
						if (in!=null) {
							try {
								in.close();
							} catch (IOException e) {
								// TODO 自动生成的 catch 块
								e.printStackTrace();
							}
						}
						if (in!=stream) {
							try {
								stream.close();
							} catch (IOException e) {
								// TODO 自动生成的 catch 块
								e.printStackTrace();
							}
						}
						if (client!=null) {
							client.getConnectionManager().shutdown();
						}
						
						client = null;
						in = null;
						stream = null;
						
					}
				}
			}
		}).start();
	}
	
	private static byte[]  readStream(InputStream stream){
		byte[]  arr = null;
		
		if (stream!=null) {
			byte[]  buf = new byte[128];//128字节，可能出现内存溢出
			int len = 0;
			
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			try {
				while((len = stream.read(buf))!=-1){
					bout.write(buf, 0, len);
				}
				arr = bout.toByteArray();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}finally{
				try {
					bout.close();
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
				bout = null;
				buf = null;
			}
		}
		
		return arr;
	}
	
}
